using Godot;
using System;

public partial class VialDropComponent : Node
{
	[Export]
	public PackedScene vialScene;//经验药瓶
	[Export]
	public HealthComponent healthComponent; //健康组件
	[Export(PropertyHint.Range, "0,1,0.01")] //只能输入0-1之间的数，浮动范围0.01
	public double dropPercent = 0.5f;
	MetaProgression metaProgression;

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		metaProgression = GetNode<MetaProgression>("/root/MetaProgression");

		healthComponent.Died += OnDied;
	}

	public void OnDied(){
		var adjustedDropPercent = dropPercent;
		int experienceGainUpgradeCount = metaProgression.GetUpgradeCount("experience_gain");
		if(experienceGainUpgradeCount>0){
			adjustedDropPercent += .1;
		}
		GD.Print("掉率:",adjustedDropPercent);
		if(vialScene == null){
			return ;
		}

		if (Owner is not Node2D){
			return ;
		}

		if(new Random().NextDouble() > adjustedDropPercent){
			return ;
		}

		Vector2 spawnPosition = (Owner as Node2D).GlobalPosition;
		Node2D vialInstance = vialScene.Instantiate() as Node2D;
		GetTree().GetFirstNodeInGroup("entities_layer").AddChild(vialInstance);
		// Owner.GetParent().AddChild(vialInstance);
		vialInstance.GlobalPosition = spawnPosition;
	}
}
